<?php

namespace Admin\Controller;

use Think\Controller;
use Admin\Model\AuthRuleModel;
use Admin\Model\AuthGroupModel;
use Think\Model;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/4
 * Time: 15:00
 */
class SpTransferproductController extends AdminController
{
    public function index()
    {
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);
        $this->display();
    }

    public  function transferlist(){
        $shopId = I('shopId');
        $sql="select * from gr_sp_transferproduct WHERE sendShopId=$shopId AND status=1 order by id desc";
        $list = $this->listsJ($sql);
        $this->assign('_list', $list);
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);
        $this->assign('shopId', $shopId);
        $this->display();
    }

    public function mylist(){
        $shopId = I('shopId');
        $sql="select * from gr_sp_transferproduct WHERE applyShopId=$shopId AND status=1 order by id desc";
        $list = $this->listsJ($sql);
        $this->assign('_list', $list);
        $shoplist = M('sp_shops')->where(array('status' => 1))->select();
        $this->assign('shoplist', $shoplist);
        $this->assign('shopId', $shopId);
        $this->display();
    }

    public  function  createmytransfer($shopId = '',$sendShopId='', $remark = "", $caigouList = ''){
        if (IS_POST) {
            $caigouArray = json_decode($caigouList);
            $gr_sp_transferproduct = array(
                'applyShopId' => $shopId,
                'sendShopId'=>$sendShopId,
                'batchNum' => NOW_TIME,
                'transferStatus' => 0,
                'createby' => UID,
                'createat' => NOW_TIME,
                'remark' => $remark,
                'status' => 1,
            );
            $transferProductId = M('sp_transferproduct')->add($gr_sp_transferproduct);
            if (!$transferProductId) {
                $result = array('code' => -1, 'msg' => '调货单创建失败！');
                $this->ajaxReturn($result);
            } else {
                for ($i = 0; $i < count($caigouArray); $i++) {
                    $gr_sp_transferproddetail = array(
                        'transferProdId' => $transferProductId,
                        'productId' => $caigouArray[$i]->pid,
                        'count' => $caigouArray[$i]->number,
                        'unit' => $caigouArray[$i]->unit,
                        'transferStockPrice' => $caigouArray[$i]->price,
                        'subTotal' => $caigouArray[$i]->amountprice,
                        'status' => 1,
                    );
                    M('sp_transferproddetail')->add($gr_sp_transferproddetail);
                }
                $result = array('code' => 1, 'msg' => '调货单创建成功！');
                $this->ajaxReturn($result);
            };

        } else {
            $map['status'] = 1;
            $pcategorylist = M('sp_pcategory')->where($map)->order('type')->select();
            int_to_string($pcategorylist);
            $this->assign('_pcategorylist', $pcategorylist);
            $shoplist = M('sp_shops')->where(array('status' => 1))->select();
            $this->assign('shopId', $shopId);
            $this->assign('shoplist', $shoplist);
            $this->display();
        }
    }

    public  function transferdetail($id=''){
        if($id){
            $Model=new Model();
            $sql="select tpd.*,p.name from gr_sp_transferproddetail tpd inner join gr_sp_product p ON tpd.productId=p.id  WHERE tpd.transferProdId=$id";
            $dt=$Model->query($sql);
            $this->assign('_list',$dt);

            $sql="select transferStatus from gr_sp_transferproduct WHERE  id=$id";
            $transferStatus=$Model->query($sql);
            $this->assign("transferStatus",$transferStatus[0]["transferStatus"]);
        }
        $this->display();
    }

    public  function transferdetailforcheck($id=''){
        if($id){
            $Model=new Model();
            $sql="select tpd.*,p.name from gr_sp_transferproddetail tpd inner join gr_sp_product p ON tpd.productId=p.id  WHERE tpd.transferProdId=$id";
            $dt=$Model->query($sql);
            $this->assign('_list',$dt);

            $sql="select transferStatus from gr_sp_transferproduct WHERE  id=$id";
            $transferStatus=$Model->query($sql);
            $this->assign("transferStatus",$transferStatus[0]["transferStatus"]);
            $this->assign("dataId",$id);
        }
        $this->display();
    }

    public  function changeStatus($dataId='',$status=''){
        if(IS_POST){
            $uid=UID;
            $checkat=NOW_TIME;
            $Model=new Model();
            $sql="update gr_sp_transferproduct set transferStatus=$status,checkby=$uid,checkat=$checkat WHERE id=$dataId";
            $res=$Model->execute($sql);
            if($res>0){
                $this->success('操作成功！');
            }else{
                $this->error('操作失败！');
            }
        }
    }
}